$(function(){
	var agentInfo=common.getLocalData('H5-agentInfo');
	var token=common.getToken();
	var getDetailUrl=common.globalHost+common.getAgentDetailPort;
	var $agentName=$('#agentName');
	var $mobile=$('#mobile');
	var $companyName=$('#companyName');
	var $headImageInput=$("#headImage");
	var $uploadBusinessCard=$("#esheyi-upload-businessCard");
	var $groupPhoto=$('#groupPhoto');
    var client = navigator.userAgent;
    var isiOS = !!client.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	var $uploadHousePhotos=$('#upload-house-photos');
	if(isiOS){
		$uploadHousePhotos.find('input').attr('multiple',true);
	}
	var uploadOptions={
        uploadType:"more",
        fileName:"groupPhoto",
        folder:"agent",
        maxlength:9,
        agentId:agentInfo.id,
        token:token,
        uploadSuccess:function (filePath,photoList) {
            $groupPhoto.val(photoList.join(","));
        },
        deleteImage:function (photoList) {
            $groupPhoto.val(photoList.join(","));
        },
        uploadFail:function (data) {

        }
    };
    var screenWidth=window.screen.width;
   	$esheyiUploadFile=$('#esheyi-upload-file');
   	$esheyiUploadFile.css({"width":screenWidth});
	common.requestFormData({
		url:getDetailUrl,
		sendData:{token:token,id:agentInfo.id},
		resSuccess:function(data){
			var data=data.res.data;
			var mobile=data['mobile'];
				mobile?$mobile.html(mobile):'';
			var agentName=data['agentName'];
				agentName?$agentName.html(agentName):'';
			var companyName=data['companyName'];
				companyName?$companyName.html(companyName):'';
			var isPerfect=data['isPerfect'];
			if(isPerfect==1){
				$.each(data['perfect'],function(key,value){
		        	$("#"+key).val(value);
				});
				_headImageBackfill(data['perfect']['headImage']);
				var uploadHousePhoto= new UploadImage($uploadHousePhotos,uploadOptions,data['perfect']['groupPhoto']);
			}else{
				_headImageBackfill();
				var uploadHousePhoto= new UploadImage($uploadHousePhotos,uploadOptions,data['perfect']['groupPhoto']);
			}
			
		},
		resFailed:function(data){
           
		},
		resError:function(data){
			
		}
	});
	function _headImageBackfill(initImageUrl){
		var headImage=new headUpload($uploadBusinessCard,{
	        agentId:agentInfo.id,
	        fileType:"headImage",
	        token:token,
	        uploadSuccess:function (resFile) {
	            headImage.uploadNotice.html("上传成功").show().delay(1000).hide(0);
	            $headImageInput.val(resFile);
	        },
	        uploadFail:function (data) {
	            headImage.uploadNotice.html("上传失败,"+data.res.msg).show().delay(1000).hide(0);
	        },
	        deleteImage:function () {
	            $headImageInput.val("");
	        }
	    },initImageUrl);
	}
    var $submitBtn=$('#submit-btn');
    var $personForm=$('#personForm');
	var $imgBox=$('.img-box');
	var url=common.globalHost+common.perfectAgentInfo;
	var $maskControl=$('#mask');
	var $alertControl=$('#alert');
	function _pressSubmit(msg){
		$maskControl.removeClass('mask-hide').addClass("mask-show");
	    $alertControl.removeClass('mask-hide').addClass("mask-show");
	    $alertControl.children('.alert-content').html(msg);
	    // 您的信息未填写完整，将不在电视看板上展示
	};
	function _pressCancel(){
		//点击取消按钮
		 $alertControl.children('.alert-cancel').off('tap').on('tap',function(e){
		    $maskControl.removeClass('mask-show').addClass("mask-hide");
		    $alertControl.removeClass('mask-show').addClass("mask-hide");
		    e.preventDefault();
		    return false;
		});
	}
	var $goBack=$('#goBack');
	$goBack.off('tap').on('tap',function(e){
		_pressSubmit("您当前的内容未保存，确认返回？");
		_pressCancel();
		$alertControl.children('.alert-sure').off('tap').on('tap',function(ev){
		    $maskControl.removeClass('mask-show').addClass("mask-hide");
		    $alertControl.removeClass('mask-show').addClass("mask-hide");
		    // window.history.back();
		    window.location.href="./previewPersonInfo.html";
		    ev.preventDefault();
		    return false;
		});
		e.stopPropagation();
        e.preventDefault();
    	return false;
	})
    $submitBtn.off('tap').on('tap',function(e){
		var $this=$(this);
		var checkCase=$this.hasClass("act-ago");
		var formData=common.getSendDataByForm($personForm);
		var flag=true;
		for(var idx in formData){
			if(formData[idx]==""||formData[idx]=='undefined'){
				flag=false;
			}
		}
		var sendData={
			token:token,
			agentId:agentInfo.id
		};
		sendData=$.extend({},sendData,formData);
		var len=$imgBox.children().length;
		var groupPhotoVal=$groupPhoto.val();
		if(groupPhotoVal==""){
			var valLength=0;
		}else{
			var valInputArr=groupPhotoVal.split(',');
			var valLength=valInputArr.length;
		}

		if(!flag){
			_pressSubmit("您的信息未填写完整，将不在电视看板上展示");
			_pressCancel();
			$alertControl.children('.alert-sure').off('tap').on('tap',function(e){
			    $maskControl.removeClass('mask-show').addClass("mask-hide");
			    $alertControl.removeClass('mask-show').addClass("mask-hide");
				sendAjax();
			    e.preventDefault();
			    return false;
			});
		}else{
			sendAjax();
		}
		function sendAjax(){
			if(!!checkCase&&valLength===len-1){
				$this.removeClass("act-ago").addClass("acted-yet");
	    		common.requestFormData({
	    			url:url,
	    			sendData:sendData,
	    			resSuccess:function(data){
						$this.removeClass("acted-yet").addClass("act-ago");
	                    setTimeout(function(){
	                        window.location.href="./previewPersonInfo.html";
	                    },2000)	
	    			},
	    			resFailed:function(data){
						$this.removeClass("acted-yet").addClass("act-ago");
	    			},
	    			resError:function(data){
						$this.removeClass("acted-yet").addClass("act-ago");
	    			}
	    		});
			}else{
				// common.errDialog('请上传照片');
			}
		}
		e.stopPropagation();
        e.preventDefault();
    	return false;
    });
    /*跳过，以后再认证*/
    var $skipMine=$('#skip-mine');
	$skipMine.off('tap').on('tap',function(e){
		_pressSubmit("亲，您是否确认离开当前页面？");
		_pressCancel();
		$alertControl.children('.alert-sure').off('tap').on('tap',function(e){
		    $maskControl.removeClass('mask-show').addClass("mask-hide");
		    $alertControl.removeClass('mask-show').addClass("mask-hide");
		    window.location.href="./previewPersonInfo.html";
		    e.preventDefault();
		    return false;
		});
		e.stopPropagation();
        e.preventDefault();
    	return false;
	})
})